// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.linqingying.gitcode.authentication.accounts

import com.intellij.collaboration.auth.PersistentDefaultAccountHolder
import com.intellij.notification.NotificationType
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.components.*
import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.VcsNotifier
import com.intellij.platform.util.coroutines.childScope
import kotlinx.coroutines.CoroutineScope
import com.linqingying.gitcode.i18n.GitCodeBundle
import com.linqingying.gitcode.util.GitCodeNotificationIdsHolder
import com.linqingying.gitcode.util.GitCodeNotifications
import com.linqingying.gitcode.util.GitCodeUtil

/**
 * Handles default GitCode account for project
 */
@Service(Service.Level.PROJECT)
@State(name = "GitCodeDefaultAccount", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)], reportStatistic = false)
class GitCodeProjectDefaultAccountHolder(project: Project, parentCs: CoroutineScope)
  : PersistentDefaultAccountHolder<GitCodeAccount>(project, parentCs.childScope()) {

  override fun accountManager() = service<GCAccountManager>()

  override fun notifyDefaultAccountMissing() = runInEdt {
    val title = GitCodeBundle.message("accounts.default.missing")
    GitCodeUtil.LOG.info("${title}; ${""}")
    VcsNotifier.IMPORTANT_ERROR_NOTIFICATION.createNotification(title, NotificationType.WARNING)
      .setDisplayId(GitCodeNotificationIdsHolder.MISSING_DEFAULT_ACCOUNT)
      .addAction(GitCodeNotifications.getConfigureAction(project))
      .notify(project)
  }
}
